
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>error: cast from ... to ... loses precision · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="warnings-treated-as-errors.html" />
    
    
    <link rel="prev" href="./" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    信息显示
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="print-predefined-macros.html">
            
                <a href="print-predefined-macros.html">
            
                    
                    打印gcc预定义的宏信息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="print-commands-only.html">
            
                <a href="print-commands-only.html">
            
                    
                    打印gcc执行的子命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="print-level-options.html">
            
                <a href="print-level-options.html">
            
                    
                    打印优化级别的对应选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="diagnostics-color.html">
            
                <a href="diagnostics-color.html">
            
                    
                    打印彩色诊断信息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="print-header-search-dir.html">
            
                <a href="print-header-search-dir.html">
            
                    
                    打印头文件搜索路径
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="print-file-name.html">
            
                <a href="print-file-name.html">
            
                    
                    打印连接库的具体路径
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="./">
            
                <a href="./">
            
                    
                    预处理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="inhibit-linemarkers.html">
            
                <a href="inhibit-linemarkers.html">
            
                    
                    生成没有行号标记的预处理文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="define-macro.html">
            
                <a href="define-macro.html">
            
                    
                    在命令行中预定义宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="undefine-macro.html">
            
                <a href="undefine-macro.html">
            
                    
                    在命令行中取消宏定义
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    汇编
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="pass-options-to-assembler.html">
            
                <a href="pass-options-to-assembler.html">
            
                    
                    把选项传给汇编器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="verbose-asm.html">
            
                <a href="verbose-asm.html">
            
                    
                    生成有详细信息的汇编文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    调试
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="address-sanitizer.html">
            
                <a href="address-sanitizer.html">
            
                    
                    利用Address Sanitizer工具检查内存访问错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="thread-sanitizer.html">
            
                <a href="thread-sanitizer.html">
            
                    
                    利用Thread Sanitizer工具检查数据竞争的问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    连接
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="pass-options-to-linker.html">
            
                <a href="pass-options-to-linker.html">
            
                    
                    把选项传给连接器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="set-dynamic-linker.html">
            
                <a href="set-dynamic-linker.html">
            
                    
                    设置动态连接器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="./">
            
                <a href="./">
            
                    
                    函数属性
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="must-emit-function-code.html">
            
                <a href="must-emit-function-code.html">
            
                    
                    禁止函数被优化掉
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="must-forceinline-function-code.html">
            
                <a href="must-forceinline-function-code.html">
            
                    
                    强制函数inline
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="./">
            
                <a href="./">
            
                    
                    常见错误
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.7.1" data-path="cast-lose-precision.html">
            
                <a href="cast-lose-precision.html">
            
                    
                    error: cast from ... to ... loses precision
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="warnings-treated-as-errors.html">
            
                <a href="warnings-treated-as-errors.html">
            
                    
                    all warnings being treated as errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="specify-dwarf-version.md">
            
                <span>
            
                    
                    gdb无法调试gcc编译的程序
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="./">
            
                <a href="./">
            
                    
                    其它
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="syntax-only.html">
            
                <a href="syntax-only.html">
            
                    
                    只做语法检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="save-temps.html">
            
                <a href="save-temps.html">
            
                    
                    保存临时文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="turn-on-warnings.html">
            
                <a href="turn-on-warnings.html">
            
                    
                    打开警告信息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="specify-language.html">
            
                <a href="specify-language.html">
            
                    
                    指定语言类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="pack-struct.html">
            
                <a href="pack-struct.html">
            
                    
                    改变结构体成员的字节对齐
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >error: cast from ... to ... loses precision</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="error-cast-from--to--loses-precision">error: cast from ... to ... loses precision</h1>
<h2 id="&#x4F8B;&#x5B50;">&#x4F8B;&#x5B50;</h2>
<pre><code>#include &lt;iostream&gt;

class Foo {
 public:
  void print() const {
    std::cout &lt;&lt; (int)(this) &lt;&lt; &quot;\n&quot;;
  }
};

int main()
{
  class Foo foo;

  foo.print();
  return 0;
}
</code></pre><h2 id="&#x6280;&#x5DE7;">&#x6280;&#x5DE7;</h2>
<p>&#x5728;g++&#x7F16;&#x8BD1;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x4F1A;&#x62A5;&#x5982;&#x4E0B;&#x9519;&#x8BEF;&#xFF1A;</p>
<pre><code>$ g++ foo.cc 
foo.cc: In member function &#x2018;void Foo::print() const&#x2019;:
foo.cc:6:28: error: cast from &#x2018;const Foo*&#x2019; to &#x2018;int&#x2019; loses precision [-fpermissive]
</code></pre><p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5F3A;&#x5236;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x7684;&#x9519;&#x8BEF;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x6E90;&#x4EE3;&#x7801;&#x4E3A;&#xFF1A;</p>
<pre><code>std::cout &lt;&lt; (int*)(this) &lt;&lt; &quot;\n&quot;;
</code></pre><p>&#x5373;&#x53EF;&#x3002;</p>
<p>&#x5982;&#x679C;&#xFF0C;&#x4F60;&#x4E0D;&#x60F3;&#xFF08;&#x6216;&#x4E0D;&#x80FD;&#xFF09;&#x53BB;&#x4FEE;&#x6539;&#x6E90;&#x7A0B;&#x5E8F;&#xFF0C;&#x53EA;&#x662F;&#x5E94;&#x4E3A;&#x5347;&#x7EA7;&#x4E86;gcc&#x800C;&#x5E26;&#x6765;&#x4E86;&#x8FD9;&#x6837;&#x7684;&#x9519;&#x8BEF;&#xFF0C;&#x90A3;&#x4E48;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>-fpermissive</code>&#x9009;&#x9879;&#xFF0C;&#x5C06;&#x9519;&#x8BEF;&#x964D;&#x4F4E;&#x4E3A;&#x8B66;&#x544A;&#xFF1A;</p>
<pre><code>$ g++ foo.cc -fpermissive
foo.cc: In member function &#x2018;void Foo::print() const&#x2019;:
foo.cc:6:28: warning: cast from &#x2018;const Foo*&#x2019; to &#x2018;int&#x2019; loses precision [-fpermissive]
</code></pre><p>&#x8BE6;&#x60C5;&#x53C2;&#x89C1;<a href="https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html#index-fpermissive-166" target="_blank">gcc&#x624B;&#x518C;</a></p>
<h2 id="&#x8D21;&#x732E;&#x8005;">&#x8D21;&#x732E;&#x8005;</h2>
<p>xmj</p>
<footer class="page-footer"><span class="copyright">powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x7AE0;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2015-04-13 10:44:43
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev " aria-label="Previous page: 常见错误">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="warnings-treated-as-errors.html" class="navigation navigation-next " aria-label="Next page: all warnings being treated as errors">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"error: cast from ... to ... loses precision","level":"1.7.1","depth":2,"next":{"title":"all warnings being treated as errors","level":"1.7.2","depth":2,"path":"warnings-treated-as-errors.md","ref":"warnings-treated-as-errors.md","articles":[]},"previous":{"title":"常见错误","level":"1.7","depth":1,"path":"README.md","ref":"README.md","articles":[{"title":"error: cast from ... to ... loses precision","level":"1.7.1","depth":2,"path":"cast-lose-precision.md","ref":"cast-lose-precision.md","articles":[]},{"title":"all warnings being treated as errors","level":"1.7.2","depth":2,"path":"warnings-treated-as-errors.md","ref":"warnings-treated-as-errors.md","articles":[]},{"title":"gdb无法调试gcc编译的程序","level":"1.7.3","depth":2,"path":"specify-dwarf-version.md","ref":"specify-dwarf-version.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["back-to-top-button","chapter-fold","hide-element","code","splitter","-lunr","-search","search-pro","insert-logo","tbfed-pagefooter","popup","livereload"],"pluginsConfig":{"tbfed-pagefooter":{"copyright":"","modify_label":"该文章修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"livereload":{},"splitter":{},"search-pro":{},"popup":{},"code":{"copyButtons":true},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"back-to-top-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"insert-logo":{"style":"background: none; max-height: 30px; min-height: 30px","url":"https://gcc.gnu.org/img/gccegg-65.png"}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"cast-lose-precision.md","mtime":"2015-04-13T02:44:43.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-03-14T14:50:05.575Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

